package edu.hawaii.myisern.action;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

/**
 * Tests operation of the Isern Action Bean.
 *
 * @author Lisa Chen
 */
public class TestIsernActionBean {
  /** Get the test host. */
  private String testHost = "http://localhost:8080/";

  /** Application Url. (created to appease PMD) */
  private String applicationName = "myisern-violet";

  /**
   * Tests holding the session after a user logs in.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testSessionManagement() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login.jsp page and check for successful retrieval
    String Url = testHost + applicationName + "/login.jsp";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Retrieving login page.", "My ISERN Login", response.getTitle());
    
    // login to MyIsern system
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    loginRequest.setParameter("username", "myisern");
    loginRequest.setParameter("password", "isern2008");
    response = conversation.getResponse(loginRequest);
    assertEquals("Check login forwarded to index.jsp", "ISERN - Organizations", 
        response.getTitle());

    // attempt to access a non public url, check the session id
    WebLink researcherLink = response.getLinkWith("Collaborations");
    String result = researcherLink.click().getTitle();
    assertEquals("Retrieving non public url after user has been validated.", 
        "ISERN - Collaborations", result);
  }
}
